home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / ka9q / expiry / hisbuild.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-15  |  2.4 KB  |  105 lines

  1. /* Rebuild the history file */
  2.  
  3. /* Written by Bernie Roehl, May 1990 */
  4.  
  5. /* Works with the NNTPCLI by Anders Klemets and Bernie Roehl */
  6.  
  7. #include <stdio.h>
  8. #include <dir.h>
  9. #include <dos.h>
  10. #include <fcntl.h>
  11. #include <sys/stat.h>
  12. #include <ctype.h>
  13.  
  14. char *progname = "HISBUILD";
  15.  
  16. void main(argc, argv)
  17. int argc;
  18. char *argv[];
  19.     {
  20.     FILE *hist;
  21.     void walk_dirs();
  22.     char buff[100];
  23.     if (argc != 3) {
  24.         printf("%s: Correct usage is 'hisbuild spooldir newsdir'\n", progname);
  25.         exit(2);
  26.         }
  27.     if (mlock(argv[2], "history")) {
  28.         printf("%s: Could not lock history file '%s\\history'\n", progname, argv[2]);
  29.         exit(1);
  30.         }
  31.     sprintf(buff, "%s/history", argv[2]);
  32.     if ((hist = fopen(buff, "w")) == NULL) {
  33.         printf("%s: Could not open history file '%s'\n", progname, buff);
  34.         rmlock(argv[2], "history");
  35.         exit(3);
  36.         }
  37.     walk_dirs(argv[1], hist);
  38.     fclose(hist);
  39.     rmlock(argv[2], "history");
  40.     exit(0);
  41.     }
  42.  
  43. void walk_dirs(char *dir, FILE *hist)
  44.     {
  45.     char work[100];
  46.     struct ffblk ff;
  47.  
  48.     sprintf(work, "%s/*.txt", dir);
  49.     if (findfirst(work, &ff, 0) == 0)
  50.         do {
  51.             FILE *tmpf;
  52.             sprintf(work, "%s/%s", dir, ff.ff_name);
  53.             if ((tmpf = fopen(work, "r")) == NULL) {
  54.                 printf("%s: Could not open '%s'\n", progname, work);
  55.                 continue;
  56.                 }
  57.             while (fgets(work, sizeof(work), tmpf))
  58.                 if (!strnicmp(work, "Message-ID:", 11)) {
  59.                     char *p;
  60.                     for (p = &work[11]; *p; ++p)
  61.                         if (!isspace(*p))
  62.                             break;
  63.                     fputs(p, hist);
  64.                     }
  65.             fclose(tmpf);
  66.             } while (findnext(&ff) == 0);
  67.  
  68.     sprintf(work, "%s/*.*", dir);
  69.     if (findfirst(work, &ff, FA_DIREC) == 0)
  70.         do {
  71.             if (ff.ff_name[0] != '.') {
  72.                 sprintf(work, "%s/%s", dir, ff.ff_name);
  73.                 walk_dirs(work, hist);
  74.                 }
  75.             } while(findnext(&ff) == 0);
  76.     }
  77.  
  78. mlock(char *dir, char *id)  /* from KA9Q */
  79.     {
  80.     char lockname[80];
  81.     int fd;
  82.  
  83.     /* Try to create the lock file in an atomic operation */
  84.     sprintf(lockname,"%s/%s.lck",dir,id);
  85. #ifdef        AMIGA
  86.     /* don't ask, really, just don't ask... I'd do file locking on
  87.      * an Amiga much more differently than this.
  88.      */
  89.     if(access(lockname, 0) == 0)
  90.         return -1;
  91. #endif
  92.     if((fd = open(lockname, O_WRONLY|O_EXCL|O_CREAT,0600)) == -1)
  93.         return -1;
  94.     close(fd);
  95.     return 0;
  96.     }
  97.  
  98. rmlock(char *dir, char *id)  /* from KA9Q */
  99.     {
  100.     char lockname[80];
  101.     sprintf(lockname,"%s/%s.lck",dir,id);
  102.     return(unlink(lockname));
  103.     }
  104.  
  105.